package com.ruoyi.web.publish.usefunds.controller;

import java.io.FileNotFoundException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.publish.usefunds.domain.Usefunds;
import com.ruoyi.web.publish.usefunds.service.IUsefundsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资金用途说明Controller
 *
 * @author houxuzhong
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/publish/usefunds/usefunds")
public class UsefundsController extends BaseController
{
    @Autowired
    private IUsefundsService usefundsService;

    /**
     * 查询资金用途说明列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Usefunds usefunds)
    {
        startPage();
        List<Usefunds> list = usefundsService.selectUsefundsList(usefunds);
        return getDataTable(list);
    }

    /**
     * 查询用户对应资金用途说明列表
     */
    @GetMapping("/listMyUsefunds")
    public TableDataInfo listMyUsefunds(Usefunds usefunds)
    {
        startPage();
        List<Usefunds> list = usefundsService.selectMyUsefundsList(usefunds);
        return getDataTable(list);
    }

    /**
     * 导出资金用途说明列表
     */
    @Log(title = "资金用途说明", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Usefunds usefunds)
    {
        List<Usefunds> list = usefundsService.selectUsefundsList(usefunds);
        ExcelUtil<Usefunds> util = new ExcelUtil<Usefunds>(Usefunds.class);
        return util.exportExcel(list, "资金用途说明数据");
    }

    /**
     * 获取资金用途说明详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(usefundsService.selectUsefundsById(id));
    }

    /**
     * 新增资金用途说明
     */
    @Log(title = "资金用途说明", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Usefunds usefunds)
    {
        return toAjax(usefundsService.insertUsefunds(usefunds));
    }

    /**
     * 修改资金用途说明
     */
    @Log(title = "资金用途说明", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Usefunds usefunds)
    {
        return toAjax(usefundsService.updateUsefunds(usefunds));
    }

    /**
     * 删除资金用途说明
     */
    @Log(title = "资金用途说明", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(usefundsService.deleteUsefundsByIds(ids));
    }

  /*  @ApiOperation("上传文件")
    @PostMapping("/upload/file")
    public AjaxResult uploadFile(MultipartFile[] file, @RequestParam(name = "type") long type, @RequestParam(name = "id") String id) throws Exception
    {
        try
        {
            if (file == null || file.length == 0) {
                throw  new FileNotFoundException("没有文件");
            }
            for(MultipartFile fileData:file) {
                String filePath = RuoYiConfig.getProfile() +"/"+ Usefunds.getUsefundsFilepath();
                // 上传并返回新文件名称
                String originalName = fileData.getOriginalFilename();
                String fileName = FileUploadUtils.upload(filePath, fileData);
                // 到这里，文件已经上传了，下面是存入数据库
                String url = serverConfig.getUrl() + fileName;
                Usefunds usefunds= new usefunds();
                usefunds.setPeopleprofileProfiletype(type);
                peopleProfiles.setId(id);
                ……
                peopleProfiles.setUrl(url);
                peopleProfilesService.insertPeopleProfiles(peopleProfiles);
            }
            return AjaxResult.success(peopleProfilesService.selectPeopleProfilesListByID(id));
            // 存入库后，再查一次，返回更新后的数据。
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }*/
}
